<template>
  <div class="top-category">
    <div class="container">
      <XtxSlider :sliders="bannerList" />
      <!-- 所有二级分类 -->
      <div class="sub-list">
        <h3>全部分类</h3>
        <ul>
          <li v-for="i in categroyAllList.children" :key="i.id">
            <a href="javascript:;">
              <img :src="i.picture" />
              <p>{{ i.name }}</p>
            </a>
          </li>

        </ul>
      </div>
      <!-- 分类关联商品 -->
          <div class="ref-goods" v-for="item in categroyAllList.children" :key="item.id">
            <div class="head">
              <h3>- {{ item.name }} -</h3>
              <XtxMore to="/" />
            </div>
            <div class="body">
              <RouterLink
                to="/"
                class="goods-item"
                v-for="good in item.goods"
                :key="good.id"
              >
                <img :src="good.picture" alt="" />
                <p class="name ellipsis">{{ good.desc }}</p>
                <p class="desc ellipsis">{{ good.name }}</p>
                <p class="price">&yen;{{ good.price }}</p>
              </RouterLink>
            </div>
          </div>
    </div>
  </div>
</template>

<script>
// 轮播图板块
// import HomeBanner from '@/views/home/components/home-banner.vue'
import { ref } from 'vue'
import { findBanner } from '@/api/home'
import { findTopCategory } from '@/api/category'
import { onBeforeRouteUpdate, useRoute } from 'vue-router'
export default {
  name: 'TopCategory',
  // components: {
  //   HomeBanner
  // },
  setup () {
    // bannerList
    // 响应式数据
    const bannerList = ref([])
    // 调用获取轮播图的函数
    async function loadBannerList () {
      // 调用获取轮播图的接口
      const res = await findBanner()
      // console.log(res.data.result)
      // 得到的数组赋值给定义好的响应式bannerList
      bannerList.value = res.data.result
    }
    // 调用获取轮播图的函数
    loadBannerList()

    // 分类信息
    // categroyAllList
    const categroyAllList = ref([])
    // 获取url中的id
    const route = useRoute() // vue3 中不能用this.$route.params.id
    // console.log(route.params)
    // 定义一个分类的函数
    // id = route.params.id  因为第一次获取分类信息没有id  所有没有id的情况下 默认是route.params.id来获取分类
    async function getCategroyAllList (id = route.params.id) {
      // 调用分类的皆接口
      const res = await findTopCategory(id)
      // console.log(res)
      // 赋值给categroyAllList
      categroyAllList.value = res.data.result
    }
    // 调用分类函数
    getCategroyAllList()

    onBeforeRouteUpdate((to) => {
      // console.log(to.params.id)
      getCategroyAllList(to.params.id)
    })

    return {
      bannerList,
      categroyAllList
    }
  }
}
</script>

<style scoped lang="less">
.goods-item {
    display: block;
    width: 220px;
    padding: 20px 30px;
    text-align: center;
    img {
      width: 160px;
      height: 160px;
    }
    p {
      padding-top: 10px;
    }
    .name {
      font-size: 16px;
    }
    .desc {
      color: #999;
      height: 29px;
    }
    .price {
      color: @priceColor;
      font-size: 20px;
    }
  }
.top-category {
  h3 {
    font-size: 28px;
    color: #666;
    font-weight: normal;
    text-align: center;
    line-height: 100px;
  }
  .sub-list {
    margin-top: 20px;
    background-color: #fff;
    ul {
      display: flex;
      padding: 0 32px;
      flex-wrap: wrap;
      li {
        width: 168px;
        height: 160px;
        a {
          text-align: center;
          display: block;
          font-size: 16px;
          img {
            width: 100px;
            height: 100px;
          }
          p {
            line-height: 40px;
          }
          &:hover {
            color: @xtxColor;
          }
        }
      }
    }
  }
  .ref-goods {
    background-color: #fff;
    margin-top: 20px;
    position: relative;
    .head {
      .xtx-more {
        position: absolute;
        top: 20px;
        right: 20px;
      }
      .tag {
        text-align: center;
        color: #999;
        font-size: 20px;
        position: relative;
        top: -20px;
      }
    }
    .body {
      display: flex;
      justify-content: space-around;
      padding: 0 40px 30px;
    }
  }
}
</style>
